<?php 
session_start();
ini_set("display_errors", 0);
////////////////////////////////////////////////////
$dbhost = 'localhost';				//服务器
$dbuser = 'liushiyu';					//数据库用户名
$dbpass = 'liu18637177517';						//数据库密码
$msgkey = '89n3kb5bj9h3gz2';		//通信密钥,必须和模块里面的解密数据使用的密钥一样
////////////////////////////////////////////////////
if(substr($_POST['DATA'],0,7)=='GETRAND'){//获取验证码
	$_SESSION[rand_temp]=get_rand();
	exit(strtoupper(strToHex(rc4($_SESSION[rand_temp],$msgkey))));
}
$temp = str_decode($_POST['DATA'],$msgkey,$_SESSION[rand_temp]);//解密数据
$temp = explode('|+|',$temp);//拆开原始数据
if(count($temp)<4){
	exit('Error');
}
$con = mysql_connect($dbhost, $dbuser, $dbpass);
if (!$con){
	echo str_encode(mysql_error(),$msgkey,$rand);
	exit;
}
mysql_select_db($temp[2],$con);
mysql_query("set names GB2312");//这里自己设置数据库的编码
$rand = $temp[0]*1020/2;//处理客户端效验码

if($temp[1]=='SQL'){//添加删除更新
	if(mysql_query($temp[3])==false){
		echo str_encode('0',$msgkey,$rand);//更改失败
	}else{
		echo str_encode('1',$msgkey,$rand);//更改成功
	}
	sql_close();
	exit;
}
if($temp[1]=='GUN'){//获取记录数
	$rs=mysql_query("select count(*) from ".$temp[3]." ".$temp[4]);
	$myrow = mysql_fetch_array($rs);
	sql_close();
	echo str_encode("$myrow[0]",$msgkey,$rand);
	exit;
}
if($temp[1]=='UPD'){//添加删除更新
	if ($temp[3]=='i'){
		$sql = "INSERT INTO ".$temp[4]." (".$temp[5].") VALUES (".$temp[6].")";
	}elseif ($temp[3]=='d'){
		$sql = "DELETE FROM  ".$temp[4]." ".$temp[5];
	}elseif ($temp[3]=='u'){
		$sql = "UPDATE ".$temp[4]." SET ".$temp[5]." ".$temp[6];
	}
	if(mysql_query($sql)==false){
		echo str_encode('0',$msgkey,$rand);//更改失败
	}else{
		echo str_encode('1',$msgkey,$rand);//更改成功
	}
	sql_close();
	exit;
}
if($temp[1]=='QUE'){//查询
	$sql="SELECT ".$temp[4]." FROM ".$temp[3]." ".$temp[5];//执行SQL
	$result = mysql_query($sql);
	while($row = mysql_fetch_row($result))
	{
		foreach ($row as $a){
   		 $txt .= $a.'/';
		}
		$txt .= '<br>';
	}
	echo str_encode($txt,$msgkey,$rand);
	sql_close();
	exit;
}


function rc4($data,$pwd)
{  
	$key[] ="";
	$box[] ="";
	$pwd_length = strlen($pwd);
	$data_length = strlen($data);
	for ($i = 0; $i < 256; $i++)
	{
		$key[$i] = ord($pwd[$i % $pwd_length]);
		$box[$i] = $i;
	}
	for ($j = $i = 0; $i < 256; $i++)
	{
		$j = ($j + $box[$i] + $key[$i]) % 256;
		$tmp = $box[$i];
		$box[$i] = $box[$j];
		$box[$j] = $tmp;
	}
	for ($a = $j = $i = 0; $i < $data_length; $i++)
	{
		$a = ($a + 1) % 256;
		$j = ($j + $box[$a]) % 256;
		$tmp = $box[$a];
		$box[$a] = $box[$j];
		$box[$j] = $tmp;

		$k = $box[(($box[$a] + $box[$j]) % 256)];
		$cipher .= chr(ord($data[$i]) ^ $k);
	}
	return $cipher;
}
function HexTostr($s){
	$r = "";
	for ( $i = 0; $i<strlen($s); $i += 2)
	{
		$x1 = ord($s{$i});
		$x1 = ($x1>=48 && $x1<58) ? $x1-48 : $x1-97+10;
		$x2 = ord($s{$i+1});
		$x2 = ($x2>=48 && $x2<58) ? $x2-48 : $x2-97+10;
		$r .= chr((($x1 << 4) & 0xf0) | ($x2 & 0x0f));
	}
	return $r;
}
function strToHex($s) {
	$r = "";
	$hexes = array ("0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f");
	for ($i=0; $i<strlen($s); $i++) {$r .= ($hexes [(ord($s{$i}) >> 4)] . $hexes [(ord($s{$i}) & 0xf)]);}
	return $r;
}
function str_decode($str,$key,$key_rand){//解密函数
	return (string)rc4(HexTostr((string)rc4(HexTostr($str),(string)$key_rand)),$key);
}
function str_encode($str,$key,$key_rand){//加密函数
	$key_temp = strToHex(rc4($str,(string)$key_rand));
	$key_temp = strtoupper(strToHex(rc4($key_temp,$key)));
	return $key_temp;
}
function sql_close(){
	mysql_close();
	$_SESSION[rand_temp]=get_rand();
	return true;
}
function get_rand(){
	for($i=0; $i<6; $i++){
	$rands.= dechex(rand(0,9));
	}
	return $rands;
}
?>